<?php
namespace app\common\model;

use think\Model;

class SystemSet extends Model
{
	protected function initialize()
	{
		parent::initialize();
	}
	public function alertWin($let,$url){

		echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
		
		echo "<script>alert('{$let}');location.href='{$url}'</script>";
	}
	public function alertLayer($let){
		echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
		$html = 'alert("'.$let.'");';
		$html .= 'parent.location.reload();';
		$html .= 'var index = parent.layer.getFrameIndex(window.name);';
		$html .= 'parent.layer.close(index);';
		// 父页面刷新
		//获取窗口索引
		echo "<script>{$html}</script>";
		return;
	}
	/**
	 * 获取图片路径
	 * 上传图片
	 * @param  $file
	 * @param  $position 此参数格式如 uploads/code，前后不带斜杠
	 * @return string
	 */
	public function uploadUrl($file,$position=''){
		$file = request()->file($file);
		// 移动到框架应用根目录/public/ 目录下
		if($file){
		    if($position) {
		        $info = $file->move(ROOT_PATH . 'public' . DS . $position);
		    } else {
			 $info = $file->move(ROOT_PATH . 'public' . DS . 'static/attachment');
		    }
		    
			if($info){
				// 输出 42a79759f284b767dfcb2a0197904287.jpg
				return $info->getSaveName();
			}else{
				// 上传失败获取错误信息
				return $file->getError();
			}
		}
	}
	/**
	 * 查单条数据、修改、新增
	 * @param $mysql
	 * @param number $id
	 */
	public function dList($mysql,$id=1){
		return SystemSet::name($mysql)->where('id',$id)->find();
	}
	public function dUpdate($mysql,$where,$data){
		return SystemSet::name($mysql)->where($where)->update($data); 
	}
	public function dInsert($mysql,$data,$let,$url){
		
		if(SystemSet::name($mysql)->insert($data)) return $this->alertWin($let, $url);
	}
	public function dFind($mysql,$where=array()){
		return SystemSet::name($mysql)->where($where)->find();
	}

/**
 * 	查询多条数据
 * @param unknown $mysql
 * @param unknown $where
 * @param string $order
 * @param string $limit
 * @param string $field
 */
	public function dLists($mysql,$where=array(),$order='',$limit='',$field='')
	{
		return SystemSet::name($mysql)->where($where)->order($order)->field($field)->limit($limit)->select()->toArray();
	}
	/**
	 * 音乐的接口
	 */
	public function postMicList($music='')
	{
		$mic_xx = $this->dLists('xt_tishi_msic');
		foreach ($mic_xx as $mic_v)
		{
			if($mic_v['xxcz']==1 && $mic_v['xxcz_open']==1){
				$xxmic='__MISC__/'.$mic_v['mp3_lj'];
			}else 
				$xxmic='';
			if($mic_v['xscz']==1 && $mic_v['xscz_open']==1){
				$xsmic='__MISC__/'.$mic_v['mp3_lj'];
			}else 
				$xsmic='';
			if($mic_v['tx']==1 &&$mic_v['tx_open']==1){
				$txmic='__MISC__/'.$mic_v['mp3_lj'];
			}else 
				$txmic='';
		}
		if($xxmic != ''){
			$where='czclass=2 and zt<1 and ydzt<1';
			$mic=SystemSet::name('chongzhi_list')->where($where)->find();
			if($mic != ''){
				$music="<embed autoplay='false' src='{$xxmic}'  width='0' height='0' />";
			}
		}
		if($xsmic != ''){
			$where = 'czclass=1 and zt<1';
			$mic=SystemSet::name('chongzhi_list')->where($where)->find();
			if($mic != ''){
				$music = $music."<embed autoplay='false' src='{$xsmic}'  width='0' height='0' />";
			}
		}
		if($txmic != ''){
			$where = 'zt<1 and ifck<1';
			$mic = SystemSet::name('tixian_list')->where($where)->find();
			if($mic != ''){
				$music = $music."<embed autoplay='false' src='{$txmic}'  width='0' height='0' />";
			}
		}
		echo $music;
	}

	/**
	 * 获取IP
	 * @return Ambigous <string, unknown>
	 */
	public function GetIP(){ 
         if (getenv("HTTP_CLIENT_IP") && strcasecmp(getenv("HTTP_CLIENT_IP"), "unknown")) 
             $ip = getenv("HTTP_CLIENT_IP"); 
         else if (getenv("HTTP_X_FORWARDED_FOR") && strcasecmp(getenv("HTTP_X_FORWARDED_FOR"), "unknown")) 
             $ip = getenv("HTTP_X_FORWARDED_FOR"); 
         else if (getenv("REMOTE_ADDR") && strcasecmp(getenv("REMOTE_ADDR"), "unknown")) 
             $ip = getenv("REMOTE_ADDR"); 
         else if (isset($_SERVER['REMOTE_ADDR']) && $_SERVER['REMOTE_ADDR'] && strcasecmp($_SERVER['REMOTE_ADDR'], "unknown")) 
             $ip = $_SERVER['REMOTE_ADDR']; 
         else 
             $ip = "unknown"; 
     	return $ip; 
	}
	
	public function CaoZuoJl($t,$cont,$time,$ip,$user){
		$data=array('rz_class'=>':rz_class','rz_test'=>':rz_test','rz_time'=>':rz_time','rz_ip'=>':rz_ip','rz_user'=>':rz_user');
		return SystemSet::name('rizhi_jl')->bind(['rz_class'=>$t,'rz_test'=>$cont,'rz_time'=>$time,'rz_ip'=>$ip,'rz_user'=>$user])->insert($data);
	}
	
	public function zczscj($je,$dml,$open){
		if($je!='' || $dml != '' || $open != ''){
			$data=['je'=>':je','cj_dml'=>':cj_dml','open'=>':open'];
			$res = SystemSet::name('xt_caijin')->bind(['je'=>$je,'cj_dml'=>$dml,'open'=>$open])->where('id',1)->update($data);
			return $this->alertWin('设置成功', "/admin/systemsettings/ht_zczscj");
		}
	}
	public function dltj($je){
		if($je!=''){
			$data=['max'=>':max'];
			$res = SystemSet::name('xt_dl')->bind(['max'=>$je])->where('id',1)->update($data);
			return $this->alertWin('设置成功', "/admin/systemsettings/ht_dailisz?id=".input('id')."&cid=".input('cid'));
		}
	}
}